package com.pi.base.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pi.base.common.BasePage;
import com.pi.base.common.BaseResult;
import com.pi.base.common.SearchPage;
import com.pi.base.entity.SysParameter;
import com.pi.base.entity.SysParameterExample;
import com.pi.base.service.ISysParameterService;
import com.pi.base.util.JsonTool;
import com.pi.base.util.ValidUtil;
@Controller
@RequestMapping("/parameter")
public class ParameterController extends BaseController{
	
	private final Logger logger = LoggerFactory.getLogger(ParameterController.class);
	@Resource
	private ISysParameterService sysParameterService;
	
	
	@RequestMapping(value="/getParameterList")
	@ResponseBody
	public String getParameterList(@ModelAttribute SearchPage searchPage,
			String qpName,String qpType)  throws Exception{
		 Integer page = (searchPage.getPage() == null) ? 1 : searchPage.getPage();
		 Integer rows = (searchPage.getRows() == null) ? 0 : searchPage.getRows(); //默认查询全部	   
			
		SysParameterExample example = new SysParameterExample();
		SysParameterExample.Criteria criteria = example.createCriteria();
		if(!ValidUtil.isBlank(qpName)){
			criteria.andNameLike(new StringBuilder("%").append(qpName).append("%").toString());
		}
		if(ValidUtil.isNumber(qpType)){
			criteria.andTypeEqualTo(Integer.valueOf(qpType));
		}
		// 顶层参数
		criteria.andLevelEqualTo(1);
		String orderSort ;
		if(!ValidUtil.isBlank(searchPage.getSort())){
			orderSort = new StringBuilder(ValidUtil.camelToUnderline(searchPage.getSort())).append(" ").append(searchPage.getOrder()).toString();
		}else{
			orderSort = " code ";
		}
		example.setOrderByClause(orderSort);
		BasePage<SysParameter> parameterPage = sysParameterService.getParameterListByExample(example, page, rows);
		return JsonTool.toJsonString(parameterPage);
	}

	
	@RequestMapping(value="/getSubParameterList")
	@ResponseBody
	public String getSubParameterList(@ModelAttribute SearchPage searchPage,
			Integer parameterId,String qpName,String qpType)  throws Exception{
		 Integer page = (searchPage.getPage() == null) ? 1 : searchPage.getPage();
		 Integer rows = (searchPage.getRows() == null) ? 0 : searchPage.getRows(); //默认查询全部	   
		
		SysParameterExample example = new SysParameterExample();
		example.createCriteria().andParentIdEqualTo(Integer.valueOf(parameterId));
		String orderSort ;
		if(!ValidUtil.isBlank(searchPage.getSort())){
			orderSort = new StringBuilder(ValidUtil.camelToUnderline(searchPage.getSort())).append(" ").append(searchPage.getOrder()).toString();
		}else{
			orderSort = " code ";
		}
		example.setOrderByClause(orderSort);
		BasePage<SysParameter> parameterPage = sysParameterService.getParameterListByExample(example, page, rows);
		return JsonTool.toJsonString(parameterPage);
	}
	
	@RequestMapping(value="/doSubParameterAdd")
	@ResponseBody
	public BaseResult doSubParameterAdd(@ModelAttribute SysParameter newParameter) throws Exception{
		if(newParameter == null || ValidUtil.isBlank(newParameter.getName())
				|| ValidUtil.isBlank(newParameter.getValue())
				|| ValidUtil.isBlank(newParameter.getCode())
				|| newParameter.getParentId() == null){
			return BaseResult.newErrorResult("参数错误");
		}
		return sysParameterService.insertSubSysParamter(newParameter);
	}
	
	@RequestMapping(value="/doSubParameterUpdate")
	@ResponseBody
	public BaseResult doSubParameterUpdate(@ModelAttribute SysParameter newParameter) throws Exception{
		if(newParameter == null || ValidUtil.isBlank(newParameter.getName())
				|| ValidUtil.isBlank(newParameter.getValue())
				|| newParameter.getParentId() == null
				|| newParameter.getId() == null){
			return BaseResult.newErrorResult("参数错误");
		}
		
		return sysParameterService.updateSubSysParamter(newParameter);
	}
	
	@RequestMapping(value="/doParameterAdd")
	@ResponseBody
	public BaseResult doParameterAdd(@ModelAttribute SysParameter newParameter) throws Exception{
		if(newParameter == null ){
			return BaseResult.newErrorResult("参数错误");
		}
		sysParameterService.insertSysParamter(newParameter);
		return BaseResult.newSuccessResult("新增成功");		
	}
	
	@RequestMapping(value="/doParameterUpdate")
	@ResponseBody
	public BaseResult doParameterUpdate(@ModelAttribute SysParameter newParameter) throws Exception{
		if(newParameter == null ){
			return BaseResult.newErrorResult("参数错误");
		}
		sysParameterService.updateSysParamter(newParameter);
		return BaseResult.newSuccessResult("更新成功");		
	}
	
	@RequestMapping(value="/doParameterRemove")
	@ResponseBody
	public BaseResult doParameterRemove(@RequestParam String parameterIds) throws Exception{
		if(ValidUtil.isBlank(parameterIds)){
			return BaseResult.newErrorResult("参数错误");
		}
		sysParameterService.deleteSysParamters(parameterIds);
		return BaseResult.newSuccessResult("删除成功");		
	}
	
}
